/**
 * Copyright (c) 2018 苍穹跃龙
 * <p>
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 * <p>
 * http://www.apache.org/licenses/LICENSE-2.0
 * <p>
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */
package com.l.web.master.system.controller;

import java.util.Arrays;
import java.util.List;

import javax.validation.Valid;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.l.core.util.WebUtils;
import com.l.core.web.RestCode;
import com.l.core.web.RestJson;
import com.l.web.master.system.entity.SysRole;
import com.l.web.master.system.entity.SysUser;
import com.l.web.master.system.service.ISysRoleService;
import com.l.web.master.system.service.ISysUserRoleService;
import com.l.web.master.system.service.ISysUserService;

/**
 * <p>
 * 用户表 前端控制器
 * </p>
 *
 * @author 严秋旺
 * @since 2018-09-21
 */
@Controller
@RequestMapping("/master/system/user")
public class SysUserController {

    @Autowired
    private ISysUserService userService;
    @Autowired
    private ISysRoleService roleService;
    @Autowired
    private ISysUserRoleService userRoleService;

    @RequestMapping({ "/", "index" })
    public String index(Model model) {
        return "master/system/user/index";
    }

    @RequestMapping("page")
    @ResponseBody
    public RestJson page(SysUser sysUser) {
        Page<SysUser> page = WebUtils.getPage();
        EntityWrapper<SysUser> wrapper = new EntityWrapper<>();
        wrapper.orderBy("create_time", false);
        page = userService.selectPage(page, wrapper);
        return RestJson.Success().page(page);
    }

    @RequestMapping("edit")
    public String edit(Model model, String id) {
        if (StringUtils.isNotEmpty(id)) {
            SysUser sysUser = userService.selectById(id);
            model.addAttribute("entity", sysUser);
            
            List<String> userRoles = userRoleService.findUserRoles(id);
            model.addAttribute("userRoles", userRoles);
        }
        
        List<SysRole> roles = roleService.findAllRoles();
        model.addAttribute("roles", roles);

        return "master/system/user/edit";
    }

    @RequestMapping(path = "save", method = RequestMethod.POST)
    @ResponseBody
    public RestJson save(@Valid SysUser sysUser,String[] roleId) {
        if (StringUtils.isEmpty(sysUser.getId())) {
            userService.insert(sysUser, roleId);
        } else {
            userService.updateById(sysUser, roleId);
        }

        return RestJson.Success().put("id", sysUser.getId());
    }

    @RequestMapping("remove")
    @ResponseBody
    public RestJson remove(String[] id) {
        if (id == null || id.length == 0) {
            return new RestJson(RestCode.FAILED.getCode(), "请设置删除id");
        }

        List<String> list = Arrays.asList(id);
        userService.deleteBatchIds(list);
        return RestJson.Success();
    }
}